package com.songcha.library_common.util

import java.io.File
import java.io.IOException

class FileUtil {
    companion object{
        @JvmStatic
        open fun deleteFileOrDirectory(file: File?): Boolean {
            try {
                if (file != null && file.isFile) {
                    return file.delete()
                }
                if (file != null && file.isDirectory) {
                    val childFiles = file.listFiles()
                    // 删除空文件夹
                    if (childFiles == null || childFiles.size == 0) {
                        return file.delete()
                    }
                    // 递归删除文件夹下的子文件
                    for (i in childFiles.indices) {
                        deleteFileOrDirectory(childFiles[i])
                    }
                    return file.delete()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return false
        }
    }
}